package com.zyiot.lbs;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import cn.waps.AdInfo;
import cn.waps.AppConnect;
import cn.waps.SDKUtils;

import com.umeng.analytics.MobclickAgent;
import com.zyiot.listview.AppItemView;

public class MoreAppActivity extends Activity {
	private final Handler mHandler = new Handler();
	Context context;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_more_app);
		context = this;
		Button btnback = (Button) findViewById(R.id.btnback);
		btnback.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
		ListView listView = (ListView) findViewById(R.id.listApp);
		// 异步加载自定义广告数据
		new GetDiyAdTask(context, listView).execute();
	}

	private class MyAdapter extends BaseAdapter {
		Context context;
		List<AdInfo> list;

		public MyAdapter(Context context, List<AdInfo> list) {
			this.context = context;
			this.list = list;
		}

		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			final AdInfo adInfo = list.get(position);

			View adatperView = null;

			try {
				adatperView = AppItemView.getInstance().getAdapterView(context, adInfo, 0, 0);

				convertView = adatperView;
				convertView.setTag(adatperView);
			} catch (Exception e) {
				e.printStackTrace();
			}

			return adatperView;
		}
	}

	private class GetDiyAdTask extends AsyncTask<Void, Void, Boolean> {

		Context context;
		ListView listView;
		List<AdInfo> list;

		GetDiyAdTask(Context context, ListView listView) {
			this.context = context;
			this.listView = listView;
		}

		@SuppressWarnings("unchecked")
		@Override
		protected Boolean doInBackground(Void... params) {

			try {
				while (true) {
					if (!new SDKUtils(context).isConnect()) {
						mHandler.post(new Runnable() {

							@Override
							public void run() {
								Toast.makeText(context, "数据获取失败,请检查网络重新加载", Toast.LENGTH_LONG).show();
								((Activity) context).finish();
							}
						});

						break;
					}
					list = AppConnect.getInstance(context).getAdInfoList();
					if (list != null) {

						mHandler.post(new Runnable() {

							@Override
							public void run() {
								listView.setAdapter(new MyAdapter(context, list));
							}
						});

						break;
					}

					try {
						Thread.sleep(500);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			return null;
		}
	}

	@Override
	public void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
	}

	@Override
	public void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
	}
}
